#include "snake.h"
#include<iostream>
#include<vector>
#include<graphics.h>
#include<conio.h>
#include<windows.h>
using namespace std;
void snake::Init()
{
	body.push_back({ 24, 16 });
	direction = { 0,0 };
	isgrow = 0;
}
void snake::grow()
{
	isgrow = 1;
}
void snake::move(vector<point>&f)
{
	point newHead = { body.front().x + direction.x,body.front().y + direction.y };
	body.insert(body.begin(), newHead);
	for (auto seg:f)
	{
		if (newHead.x==seg.x&&newHead.y==seg.y)
		{
			snake::grow();
		}
	}
	if (!isgrow)
	{
		body.pop_back();
	}
	else
	{
		isgrow = 0;
	}
}
point snake::getHead()
{
	return body.front();
}
bool snake::checkPz()
{
	point head = snake::getHead();
	if (head.x <= 0 || head.x >= 48|| head.y < 3 || head.y >= 32)
	{
		return 1;
	}
	for (size_t i = 1; i < body.size(); i++)
	{
		if (head.x == body[i].x && head.y == body[i].y) 
		{
			return 1;
		}
	}
	return 0;
}
void snake::draw()
{
	setfillcolor(BLUE);
	for (const auto &segment:body)
	{
		solidrectangle(segment.x*20,segment.y*20,(segment.x+1)*20,(segment.y+1)*20);
	}
}
void snake::setDirection(int dx, int dy)
{
	if (dx!=-direction.x||dy!=-direction.y)
	{
		direction = { dx,dy };
	}
}